using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SheLife.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class BasalBodyTemperatureController : ControllerBase
    {
        private readonly IBasalBodyTemperatureService _basalBodyTemperatureService;

        public BasalBodyTemperatureController(IBasalBodyTemperatureService basalBodyTemperatureService)
        {
            _basalBodyTemperatureService = basalBodyTemperatureService;
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<BasalBodyTemperature>> GetBasalBodyTemperature(int id)
        {
            var temperature = await _basalBodyTemperatureService.GetBasalBodyTemperatureByIdAsync(id);
            if (temperature == null)
            {
                return NotFound();
            }
            return temperature;
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<IEnumerable<BasalBodyTemperature>>> GetBasalBodyTemperaturesByUserId(int userId)
        {
            var temperatures = await _basalBodyTemperatureService.GetBasalBodyTemperaturesByUserIdAsync(userId);
            return Ok(temperatures);
        }

        [HttpGet("user/{userId}/daterange")]
        public async Task<ActionResult<IEnumerable<BasalBodyTemperature>>> GetBasalBodyTemperaturesByDateRange(
            int userId, 
            [FromQuery] DateTime startDate, 
            [FromQuery] DateTime endDate)
        {
            var temperatures = await _basalBodyTemperatureService.GetBasalBodyTemperaturesByDateRangeAsync(userId, startDate, endDate);
            return Ok(temperatures);
        }

        [HttpGet("user/{userId}/chart")]
        public async Task<ActionResult<object>> GetBasalBodyTemperatureChartData(int userId)
        {
            var chartData = await _basalBodyTemperatureService.GetBasalBodyTemperatureChartDataAsync(userId);
            return Ok(chartData);
        }

        [HttpGet("user/{userId}/ovulation/{date}")]
        public async Task<ActionResult<bool>> IsOvulationDayByTemperature(int userId, DateTime date)
        {
            var isOvulationDay = await _basalBodyTemperatureService.IsOvulationDayByTemperatureAsync(userId, date);
            return Ok(isOvulationDay);
        }

        [HttpPost]
        public async Task<ActionResult<BasalBodyTemperature>> CreateBasalBodyTemperature(BasalBodyTemperature temperature)
        {
            var createdTemperature = await _basalBodyTemperatureService.CreateBasalBodyTemperatureAsync(temperature);
            return CreatedAtAction(nameof(GetBasalBodyTemperature), new { id = createdTemperature.Id }, createdTemperature);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateBasalBodyTemperature(int id, BasalBodyTemperature temperature)
        {
            if (id != temperature.Id)
            {
                return BadRequest();
            }

            await _basalBodyTemperatureService.UpdateBasalBodyTemperatureAsync(temperature);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteBasalBodyTemperature(int id)
        {
            await _basalBodyTemperatureService.DeleteBasalBodyTemperatureAsync(id);
            return NoContent();
        }
    }
}